#include "videosettingsdialog.h"
#include "ui_videosettingsdialog.h"

#include "mainwindow.h"




NES_Palette* VideoSettingsDialog::Palettes(int Index){

  switch(Index){
    case 1:
      return NES_Palette::_2C02;
    case 2:
      return NES_Palette::_2C03_2C05;
    case 3:
      return NES_Palette::RC2C03B;
    case 4:
      return NES_Palette::_2C04;
    case 5:
      return NES_Palette::RP2C04_0001;
    case 6:
      return NES_Palette::RP2C04_0002;
    case 7:
      return NES_Palette::RP2C04_0003;
    case 8:
      return NES_Palette::RP2C04_0004;
    case 0:
    default:
      break;
  }

  return NES_Palette::Default;
}




VideoSettingsDialog::VideoSettingsDialog(void* mainWindow,QWidget* parent):QDialog(parent),ui(new Ui::VideoSettingsDialog){
  ui->setupUi(this);

  this->mainWindow=mainWindow;
  IsInit=false;

  ui->comboBox_Mult->addItem("1");
  ui->comboBox_Mult->addItem("2");
  ui->comboBox_Mult->addItem("3");
  ui->comboBox_Mult->addItem("4");

  Mult=((MainWindow*)mainWindow)->GetDisplayMult();
  ui->comboBox_Mult->setCurrentIndex(Mult-1);


  ui->comboBox_Palette->addItem("Default");
  ui->comboBox_Palette->addItem("2C02");
  ui->comboBox_Palette->addItem("2C03_2C05");
  ui->comboBox_Palette->addItem("RC2C03B");
  ui->comboBox_Palette->addItem("2C04");
  ui->comboBox_Palette->addItem("RP2C04_0001");
  ui->comboBox_Palette->addItem("RP2C04_0002");
  ui->comboBox_Palette->addItem("RP2C04_0003");
  ui->comboBox_Palette->addItem("RP2C04_0004");

  PaletteIndex=FindPalettesIndex(((MainWindow*)mainWindow)->nes->GetPalette());
  ui->comboBox_Palette->setCurrentIndex(PaletteIndex);


  ui->comboBox_TVStandards->addItem("NTSC");
  ui->comboBox_TVStandards->addItem("PAL");
  ui->comboBox_TVStandards->addItem("Dendy");

  ui->comboBox_TVStandards->setCurrentIndex(((MainWindow*)mainWindow)->GetTVStandards());


  ui->spinBox_VblankNMI_Delay->setValue(((MainWindow*)mainWindow)->GetVblankNMI_Delay());


  ui->spinBox_MMC3_CountDelay->setValue(((MainWindow*)mainWindow)->GetMMC3_CountDelay());



  IsInit=true;
}

VideoSettingsDialog::~VideoSettingsDialog(){
  delete ui;
}




void VideoSettingsDialog::closeEvent(QCloseEvent* event){

  Q_UNUSED(event);

  ((MainWindow*)mainWindow)->SetTVStandards(ui->comboBox_TVStandards->currentIndex());

  ((MainWindow*)mainWindow)->resize(((MainWindow*)mainWindow)->minimumSizeHint());
  
  ((MainWindow*)mainWindow)->VideoSettingsDialogCloseEvent();
}




void VideoSettingsDialog::on_comboBox_Mult_currentIndexChanged(int index){
  
  if(!IsInit){
    return;
  }

  Mult=index+1;
  ((MainWindow*)mainWindow)->SetDisplayMult(Mult);
}

void VideoSettingsDialog::on_comboBox_Palette_currentIndexChanged(int index){
  
  if(!IsInit){
    return;
  }
  
  PaletteIndex=index;
  ((MainWindow*)mainWindow)->nes->SetPalette(Palettes(PaletteIndex));
}

void VideoSettingsDialog::on_spinBox_VblankNMI_Delay_valueChanged(int arg1){
  
  if(!IsInit){
    return;
  }
  
  ((MainWindow*)mainWindow)->SetVblankNMI_Delay(arg1);

}

void VideoSettingsDialog::on_spinBox_MMC3_CountDelay_valueChanged(int arg1){
  
  if(!IsInit){
    return;
  }

  ((MainWindow*)mainWindow)->SetMMC3_CountDelay(arg1);

}




int VideoSettingsDialog::FindPalettesIndex(NES_Palette* palette){
  for(int i=0;i<NumOfPalette;i++){
    if(Palettes(i)==palette){
      return i;
    }
  }

  return 0;
}



